package com.sfh.cpc.mass;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.sfh.cpc.R;
import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.ImageLoaderEngine;
import com.sfh.cpc.com.entity.EMass;
import com.sfh.cpc.com.entity.RspBase;
import com.sfh.cpc.com.req.ReqMassUpdate;
import com.sfh.cpc.com.ui.SelectTakeImageActivity;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.mass.model.MassEditModel;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;

import java.util.List;

/***
 * 编辑
 */
public class MassEditActivity extends SelectTakeImageActivity<MassEditModel> {
    public static void startUI(Context context, long massId) {

        Intent intent = new Intent(context, MassEditActivity.class);
        intent.putExtra("ID", massId);
        context.startActivity(intent);
    }

    WidgetBaseToolbar toolbar;
    ImageView ivHeader;
    EditText etTitle;
    EditText etDesc;
    RadioButton rbYES, rbNO;
    ReqMassUpdate reqMassAdd = new ReqMassUpdate();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mass_edit_activity);
        this.toolbar = findViewById(R.id.toolbar);
        this.ivHeader = findViewById(R.id.ivHeader);
        this.etTitle = findViewById(R.id.etTitle);
        this.etDesc = findViewById(R.id.etDesc);
        this.rbYES = findViewById(R.id.rbYES);
        this.rbNO = findViewById(R.id.rbNO);
        long massId = getIntent().getLongExtra("ID", -1L);

        this.getViewModel().getMass(massId);
    }

    public void onViewCilcked(View view) {
        if (view == ivHeader) {
            this.hasPermissions();
        } else {
            this.reqMassAdd.setVip(this.rbYES.isChecked() ? "1" : "0");
            this.reqMassAdd.setTitle(this.etTitle.getText().toString());
            this.reqMassAdd.setContent(this.etDesc.getText().toString());
            this.getViewModel().saveMass(this.reqMassAdd);
        }
    }

    @Override
    public void onSelectImageOK(List<String> select) {
        this.getViewModel().updateFile(select.get(0));
    }

    @LiveDataMatch
    public void onMassInfo(EMass mass) {
        if (mass == null) {
            return;
        }
        this.reqMassAdd.setMassId(mass.getMassId());
        this.reqMassAdd.setUrl(mass.getMassUrl());

        this.toolbar.setTitle(mass.getMassTitle());
        new ImageLoaderEngine(this, BaseAppContent.getImageUrl(mass.getMassUrl())).resizeDimen(R.dimen.px_200, R.dimen.px_200).centerCrop().into(ivHeader);
        if (TextUtils.equals("1", mass.getNoteVip())) {
            this.rbYES.setChecked(true);
        } else {
            this.rbNO.setChecked(true);
        }

        this.rbNO.setChecked(!TextUtils.equals("1", mass.getNoteVip()));
        this.etTitle.setText(mass.getMassTitle());
        this.etDesc.setText(mass.getMassContent());
    }

    @LiveDataMatch
    public void onSaveFile(RspBase<String> data) {
        if (data.success()) {
            this.reqMassAdd.setUrl(data.getData());
            new ImageLoaderEngine(this, BaseAppContent.getImageUrl( data.getData())).resizeDimen(R.dimen.px_100, R.dimen.px_100).centerCrop().into(ivHeader);
        } else {
            this.showToast(data.getMsg());
        }
    }

    @LiveDataMatch
    public void onSaveMassSuccess() {
        finish();
    }
}
